<template>
  <div>
    <el-container>
      <el-header style="height: 120px;">
        <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 18px">
          <el-breadcrumb-item :to="{ path: '/' }" style="font-weight: bold;color: black"><i class="el-icon-s-promotion" style="margin-right: 5px"></i>后台管理</el-breadcrumb-item>
          <el-breadcrumb-item>属性模板管理</el-breadcrumb-item>
        </el-breadcrumb>
        <el-divider></el-divider>
        <div>
          <el-button type="primary" size="medium" @click="$router.push('/sys-admin/product/album/add-new')">添加属性模板</el-button>
        </div>
      </el-header>
      <el-main>
        <el-table
            :data="tableData"
            style="width: 100%"
        >
          <el-table-column
              prop="id"
              label="id"
              width="80px"
              align="center"
          >
          </el-table-column>
          <el-table-column
              prop="name"
              label="名称"
              width="200px"
              align="center"
              show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
              prop="keywords"
              label="关键词"
              show-overflow-tooltip
              header-align="center"
          >
          </el-table-column>
          <el-table-column
              prop="sort"
              label="序号"
              width="100px"
              align="center"
          >
          </el-table-column>

          <el-table-column label="查看图片"
                           width="120px"
                           align="center"
          >
            <template slot-scope="scope">
              <el-button
                  size="mini"
                  type="primary"
              >查看</el-button>
            </template>
          </el-table-column>

          <el-table-column label="操作"
                           align="center"
                           width="120px"
          >
            <template slot-scope="scope">
              <el-button
                  size="mini"
                  circle
                  icon="el-icon-edit"
                  type="success"
                  @click="handleGetStandard(scope.row)"
              ></el-button>
              <el-button
                  size="mini"
                  type="danger"
                  circle
                  icon="el-icon-delete"
                  @click="openDeleteConfirm(scope.row)"
              ></el-button>
            </template>
          </el-table-column>

        </el-table>
        <el-pagination
            style="text-align: right;margin-right: 15px;margin-top: 15px"
            layout="total, prev, pager, next"
            :total="total"
            :page-size="pageSize"
            :current-page="currentPage"
            @current-change="changePage"
        >
        </el-pagination>
      </el-main>
    </el-container>

    <el-dialog title="修改相册" :visible.sync="dialogFormVisible">
      <el-form :model="attributeTemplateStandard" :rules="rules" ref="ruleForm" label-width="120px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="attributeTemplateStandard.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="拼音" prop="pinyin">
          <el-input v-model="attributeTemplateStandard.pinyin" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="关键词" prop="keywords">
          <el-input v-model="attributeTemplateStandard.keywords" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="序号" prop="sort">
          <el-input v-model="attributeTemplateStandard.sort" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleUpdateAttributeTemplate">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      total: 0,
      pageSize: 0,
      currentPage: 1,
      dialogFormVisible: false,
      attributeTemplateStandard: {
        id: '',
        name: '',
        pinyin: '',
        keywords: '',
        sort: ''
      },
      rules: {
        name: [
          {required: true, message: '请输入名称', trigger: 'blur'},
          {min: 2, max: 25, message: '长度在 2 到 25 个字符', trigger: 'blur'}
        ],
        pinyin: [
          {required: true, message: '请输入拼音', trigger: 'blur'}
        ],
        keywords: [
          {required: true, message: '请输入关键词', trigger: 'blur'}
        ],
        sort: [
          {required: true, message: '请输入排序序号', trigger: 'blur'},
          {pattern: /^(\d{1}|[1-9]{1}[0-9]?)$/, message: '排序序号必须是 0~99 之间的数字', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    handleGetStandard(attributeTemplate) {

      let url = "http://localhost:9190/attribute-template/getStandard?id=" + attributeTemplate.id;
      console.log('请求路径为：' + url);

      let localJwt = localStorage.getItem("localJwt");
      this.axios.create({
        'headers': {
          'Authorization': localJwt
        }
      }).get(url).then((response) => {
        let jsonResult = response.data;

        if (jsonResult.state == 20000) {
          this.attributeTemplateStandard = jsonResult.data;
          console.log("根据id查到的相册数据为：" + this.attributeTemplateStandard);
          this.dialogFormVisible = true;
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadAttributeTemplateList();
            }
          });
        }
      });
    },
    handleUpdateAttributeTemplate() {
      let url = "http://localhost:9190/attribute-template/update";
      console.log('请求路径为：' + url);

      let formData = this.qs.stringify(this.attributeTemplateStandard);
      console.log('formData:' + formData);

      let localJwt = localStorage.getItem("localJwt");
      this.axios.create({
        'headers': {
          'Authorization': localJwt
        }
      }).post(url, formData).then((response) => {
        let jsonResult = response.data;

        if (jsonResult.state == 20000) {
          this.$message({
            message: '修改属性模板成功！',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.loadAttributeTemplateList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadAttributeTemplateList();
            }
          });
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    openDeleteConfirm(attributeTemplate) {
      this.$confirm('此操作将永久删除【' + attributeTemplate.id + '-' + attributeTemplate.name + '】相册，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDeleteAttributeTemplate(attributeTemplate);
      }).catch(() => {
      });
    },
    handleDeleteAttributeTemplate(attributeTemplate) {
      let url = "http://localhost:9190/attribute-template/delete?id=" + attributeTemplate.id;
      console.log('请求的路径路径为：' + url);

      let localJwt = localStorage.getItem("localJwt");
      this.axios.create({
        'headers': {
          'Authorization': localJwt
        }
      }).post(url).then((response) => {
        let jsonResult = response.data;

        if (jsonResult.state == 20000) {
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.loadAttributeTemplateList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadAttributeTemplateList();
            }
          });
        } else if (jsonResult.state == 40900) {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    changePage(value) {
      this.$router.replace("?pageNum="+value)
      this.loadAttributeTemplateList();
    },
    loadAttributeTemplateList() {
      let pageNum = this.$router.currentRoute.query.pageNum;
      if (pageNum == null) {
        pageNum = 1;
      }

      let url = "http://localhost:9190/attribute-template/list?pageNum=" + pageNum;
      console.log('请求路径为：' + url);

      let localJwt = localStorage.getItem("localJwt");
      this.axios.create({
        'headers': {
          'Authorization': localJwt
        }
      }).get(url).then((response) => {
        let jsonResult = response.data;
        console.log('返回的数据为：' + jsonResult.state);

        if (jsonResult.state == 20000) {
          console.log(jsonResult.data)
          this.tableData = jsonResult.data.list;
          this.total = jsonResult.data.total;
          this.pageSize = jsonResult.data.pageSize;
          this.currentPage = jsonResult.data.currentPage;
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    }
  },
  mounted() {
    this.loadAttributeTemplateList();
  }
}
</script>